$(document).ready(function () {
	$("input").keypress(function(e){
		if ( e.which == 13 ) return false;
	});
	//Select city
	$("#headerTopContent .citySelect").hover(
		function () {
			$("#headerTopContent .citySelect .cityListing").show();
			/*$(document).bind("click", function (event) {
				//Check select city
			    if ( !$(event.target).hasClass('cityText') && !$(event.target).parents().hasClass('cityListing')) {
			    	$("#headerTopContent .citySelect .cityListing").fadeOut(100);
			    	$(document).unbind("click");
			    }
			});*/
		},
		function () {
			$("#headerTopContent .citySelect .cityListing").hover(
				function () {},
				function () {
					$("#headerTopContent .citySelect .cityListing").hide();
				}
			);
			
			$("#headerTopContent .citySelect .cityListing").hide();
		}
	);
	
	
	//Close message
	$(".notification-s img, .notification-w img, .notification-e img").click(function () {
		$(this).parent().hide();
	});
	
	//Tab change
	$(".menuTab li a").click(function () {
		//Set current is class active
		var id = $(this).attr("href");
		activeTab(id);
		
		return false;
	});
	
	//Go top
	$("#gotop").click(function () {
		$('html, body').animate({ scrollTop: 0 }, 'slow');
		return false;
	});
	
	//Register email
	var txtRegEmail = $(".registerEmail input[type=text]").val();
	$(".registerEmail input[type=text]").focus(function () {
		if ($(this).val() == txtRegEmail) {
			$(this).val("");
		}
	});
	$(".registerEmail input[type=text]").focusout(function () {
		if ($(this).val() == "") {
			$(this).val(txtRegEmail);
		}
	});
	
	// Welcome popup step 1
	$('#popup-step1').dialog({
		autoOpen: false,
		modal: true,
		width: 780,
		resizable: false,
		draggable: false,
		dialogClass: "popupGroupOn popup-step1",
		closeOnEscape: false,
		closeText: 'hide'
	});
	
	if ($.cookie("popup-step") == null) {
		$('#popup-step1').dialog("open");
	}
	
	//Event popup step 1
	$("#popup-step1 .citySelect .cityText").click(function () {
		$("#popup-step1 .citySelect .cityListing").fadeIn(100);
		$(document).bind("click", function (event) {
			//Check select city
		    if ( !$(event.target).hasClass('cityText') && !$(event.target).parents().hasClass('cityListing')) {
		    	$("#popup-step1  .citySelect .cityListing").fadeOut(100);
		    	$(document).unbind("click");
		    }
		});
	});
	
	$("#popup-step1 .citySelect .cityListing li").click(function () {
		var val = $(this).attr("title");
		$("#popup-step1 .citySelect .cityText").val($(this).html());
		$(".registerEmail input[name=city]").val(val);
		$("#popup-step1  .citySelect .cityListing").fadeOut(100);
	});
	
	var strEmail = $("#popup-step1 input[name=email]").val();
	
	$("#popup-step1 input[name=email]").focus(function () {
		if ($(this).val() == strEmail) {
			$(this).val("");
		}
	});
	$("#popup-step1 input[name=email]").focusout(function () {
		if ($(this).val() == "") {
			$(this).val(strEmail);
		}
	});
	
	$("#popup-step1 .citySelect .cityText").focus();
	
	$("#popup-step1 a.button").click(function () {
		if ($("#popup-step1 .citySelect input[name=city]").val() == "") {
			$("#popup-step1 .citySelect .error").show();
			$("#popup-step1 .citySelect").addClass("error");			
		} else {
			$("#popup-step1 .citySelect .error").hide();
			$("#popup-step1 .citySelect").removeClass("error");
		}
		
		var email = $("#popup-step1 input[name=email]");
		var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
		if (email.val() == "" || !pattern.test(email.val())) {
			$("#popup-step1 .email .error").show();
			$("#popup-step1 .email").addClass("error");		
		} else {
			$("#popup-step1 .email .error").hide();
			$("#popup-step1 .email").removeClass("error");
		}
		
		if (!$("#popup-step1 .citySelect").hasClass("error") && !$("#popup-step1 .email").hasClass("error")) {
			var txtemail = email.val();
			$("#headerTopContent .registerEmail input[name=email]").val(email.val());
			
			$('#popup-step1').dialog("close");
			$(".registerEmail form").submit();
			$.cookie("popup-step", true);
		}
		
		
		return false;
	});
	
	$("#popup-step1 .closeWelcome").click(function () {
		$('#popup-step1').dialog("close");
		$.cookie("popup-step", true);
	});
});

function activeTab(id) {
	$(".menuTab li").removeClass("active");
	$(".menuTab li a[href=" + id+"]").parent().addClass("active");
	//Display content active
	$(".menuTabContent > div").hide();
	$(".menuTabContent " + id).show();
}